package com.cfw.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ProcessUtil {
	public static final SimpleDateFormat RAW_DATE_FORMAT = new java.text.SimpleDateFormat("yyyyMMdd");
	
	private static final Logger logger = LoggerFactory.getLogger(ProcessUtil.class);
	
		public static int run(List<String> cmd, String workingdir) {
			logger.debug("*** cmd:" + cmd);
			
			int result = 1;
			
			Process process;
			
			ProcessBuilder builder = new ProcessBuilder(cmd);
			
			if (StringUtils.isNotEmpty(workingdir)) {
				builder.directory(new File(workingdir));
			}
			
			try {
				process = builder.start();
				final InputStream is = process.getInputStream();
				final InputStream es = process.getErrorStream();
				
				new Thread(new Runnable() {
					public void run() {
						try {
							BufferedReader reader = new BufferedReader(new InputStreamReader(is));
							String line;
							while ((line = reader.readLine()) != null) {
								logger.debug(line);
							}
						} catch (IOException e) {
							e.printStackTrace();
						} finally {
							try { is.close(); } catch (IOException e) { e.printStackTrace(); }
						}
					}
				}).start();
				
				new Thread(new Runnable() {
					public void run() {
						try {
							BufferedReader reader = new BufferedReader(new InputStreamReader(es));
							String line;
							while ((line = reader.readLine()) != null) {
								logger.error(line);
							}
						} catch (IOException e) {
							e.printStackTrace();
						} finally {
							try { es.close(); } catch (IOException e) { e.printStackTrace(); }
						}
					}
				}).start();
				
				try {
					process.waitFor();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				
				result = process.exitValue();
			} catch (IOException e) {
				e.printStackTrace();
			}
			
			return result;			
		}
		
	public static int runJava(Map<String, String> param) {
		List<String> cmd = new ArrayList<String>();
		
		if (param.containsKey("java")) {
			cmd.add(param.get("java"));
		}

		if (param.containsKey("xms")) {
			cmd.add("-Xms" + param.get("xms"));
		}
		
		if (param.containsKey("xmx")) {
			cmd.add("-Xmx" + param.get("xmx"));
		}
		
		if (param.containsKey("classpath")) {
			cmd.add("-classpath");
			cmd.add(param.get("classpath"));
		}
		
		cmd.add("-Dfile.encoding=UTF-8");
		
		if (param.containsKey("class")) {
			cmd.add(param.get("class"));
		}
		
		int argsCount = 0;
		if (param.containsKey("arg-count")) {
			String argCountStr = param.get("arg-count");
			if (StringUtils.isNotEmpty(argCountStr)) {
				argsCount = Integer.parseInt(argCountStr);
			}
		}
		
		if (argsCount > 0) {
			for (int i = 1; i <= argsCount; i++) {
				String arg = param.get("arg" + Integer.toString(i));
				if (StringUtils.isNotEmpty(arg)) { cmd.add(arg); }
			}
		}
		
		String workingdir = null;
		
		if (param.containsKey("workingdir")) { workingdir = param.get("workingdir"); }
		
		return run(cmd, workingdir);
	}
	
	public static int runCmd(Map<String, String> param) {
		List<String> cmd = new ArrayList<String>();
		
		int argsCount = 0;
		if (param.containsKey("arg-count")) {
			String argCountStr = param.get("arg-count");
			if (StringUtils.isNotEmpty(argCountStr)) {
				argsCount = Integer.parseInt(argCountStr);
			}
		}
		
		if (argsCount > 0) {
			for (int i = 1; i <= argsCount; i++) {
				String arg = param.get("arg" + Integer.toString(i));
				if (StringUtils.isNotEmpty(arg)) { cmd.add(arg); }
			}
		}
		
		String workingdir = null;
		
		if (param.containsKey("workingdir")) { workingdir = param.get("workingdir"); }
		
		return run(cmd, workingdir);
	}
	
	
	public static String getDateStr() { return RAW_DATE_FORMAT.format(new Date()); }
}
